package com.wanshi.controller;


import com.wanshi.entity.User;
import com.wanshi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    //1.根据id查询用户
    @GetMapping("/{id}")
    public User findById(String id) {
        return userService.getById(id);
    }

    //2.查询所有用户
    @GetMapping("/list")
    public List list() {
        return userService.list();
    }

    //3.新增用户
    @PostMapping
        public String addUser(@RequestBody User user) {
            userService.save(user);
            return "添加成功";
    }

    //4.根据id查询用户 及 对应的订单
    @GetMapping("/orders/{id}")
    public User findUserAndOdersById(@PathVariable String id) {
       User user = userService.getById(id);

       user.setOrders(null);
       return user;
    }

}

